package cn.yusei.teamTribe.dao.user;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import cn.yusei.teamTribe.dao.user.po.UserPO;

@Component
public interface UserDAO {

	final String USER_TABLE = "e_user";
	final String USER_AUTH_TABLE = "e_user_auth";
	final String INSERT_FIELD = "username, password, salt";
	final String SELECT_FIELDS = "id, username, password, salt";
	
	@Insert({"insert into ", USER_TABLE, "(", INSERT_FIELD, ") values(#{username}, #{password}, #{salt})"})
	void insert(UserPO user);
	
	@Select({"select ", SELECT_FIELDS, " from ", USER_TABLE, " where id = #{id} limit 1"})
	UserPO selectById(@Param("id") int id);
	
	@Select({"select ", SELECT_FIELDS, " from ", USER_TABLE, " where username = #{username}"})
	UserPO selectByUsername(@Param("username") String username);
	
	@Select({"select ", USER_TABLE, ".", SELECT_FIELDS,
		" from ", USER_TABLE, ", ", USER_AUTH_TABLE,
		" where ", USER_TABLE, ".id = ", USER_AUTH_TABLE, ".id",
		" and ", USER_AUTH_TABLE, ".email = #{email}"})
	UserPO selectByEmail(@Param("email") String email);
	
	@Select({"select ", USER_TABLE, ".", SELECT_FIELDS,
		" from ", USER_TABLE, ", ", USER_AUTH_TABLE,
		" where ", USER_TABLE, ".id = ", USER_AUTH_TABLE, ".id",
		" and ", USER_AUTH_TABLE, ".phone = #{phone}"})
	UserPO selectByPhone(@Param("phone") String phone);
	
	@Update({"update ", USER_TABLE, " set password = #{password}, salt = #{salt} where id = #{id}"})
	void update(UserPO user);
	
	@Select({"select id from ",USER_TABLE,"where username like concat(concat('%',#{name}),'%')"})
	List<Integer> selectIdByName(@Param("name")String name);

	
}
